package com.atguigu.blog.controller;

import com.atguigu.blog.entity.Article;
import com.atguigu.blog.entity.Category;
import com.atguigu.blog.service.ArticleService;
import com.atguigu.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //列表
    /**
     * 查询数据
     * @author Petrichor
     * @date 2023/10/23 11:51
     * @return
     **/
    @GetMapping("/list")
    public Result getList() {
        //调用service的数据list集合
        List<Article> list = articleService.getList() ;

        return Result.ok(list);
    }
    /**
     * 1.1 文章列表展现
     * @author Petrichor
     * @date 2023/10/23 15:22
     **/
    @GetMapping("/getArticle")
    public Result getArticle(Long cid){
        List<Article> articleList = articleService.getArticle(cid);
        return  Result.ok(articleList);
    }
    /**
     * 1.3 根据文章ID查询详情信息
     * @author Petrichor
     * @date 2023/10/23 17:36
     **/
    @GetMapping("/findArticleById")
    public Result findArticleById(Long id){
        Article article = articleService.findArticleById(id);
        if(article !=null){
            return Result.ok(article);
        }else{
            return Result.fail();
        }
    }
    /**
     * 3.2 文章新增实现
     * @author Petrichor
     * @date 2023/10/24 14:47
     **/
    @PostMapping("/saveArticle")
    public Result saveArticle(@RequestBody Article article){
        articleService.saveArticle(article);
        return Result.ok();
    }
    /**
     * 3.3 文章删除
     * @author Petrichor
     * @date 2023/10/24 16:18
     **/
    @DeleteMapping("/deleteArticleById")
    public Result deleteArticleById(Long id){
        articleService.deleteArticleById(id);
        return Result.ok();
    }
    /**
     * 文章修改
     * @author Petrichor
     * @date 2023/10/24 16:18
     **/
    @PutMapping("/updateArticle")
    public Result updateArticle(@RequestBody Article article){
        articleService.updateArticle(article);
        return Result.ok();
    }
}
